/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.dialect;

import java.io.Serializable;

import org.qdss.persist.dialect.editor.BinaryEditor;
import org.qdss.persist.dialect.editor.BoolEditor;
import org.qdss.persist.dialect.editor.CharEditor;
import org.qdss.persist.dialect.editor.DateEditor;
import org.qdss.persist.dialect.editor.DateTimeEditor;
import org.qdss.persist.dialect.editor.DecimalEditor;
import org.qdss.persist.dialect.editor.DoubleEditor;
import org.qdss.persist.dialect.editor.IntEditor;
import org.qdss.persist.dialect.editor.LongEditor;
import org.qdss.persist.dialect.editor.ReferenceListEditor;
import org.qdss.persist.dialect.editor.NTextEditor;
import org.qdss.persist.dialect.editor.PrimaryEditor;
import org.qdss.persist.dialect.editor.ReferenceEditor;
import org.qdss.persist.dialect.editor.SmallIntEditor;
import org.qdss.persist.dialect.editor.StringEditor;
import org.qdss.persist.dialect.editor.TextEditor;
import org.qdss.persist.dialect.editor.TimestampEditor;
import org.qdss.persist.dialect.editor.TinyIntEditor;

/**
 * 字段类型定义
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @see 0.1, May 25, 2009
 * @version $Id: FieldType.java 144 2012-08-30 15:28:06Z zhaofang123@gmail.com $
 */
public class FieldType implements Type, Serializable {
	private static final long serialVersionUID = 1L;

	// -------------------------------------------------------------------------------------
	
	public static final int NO_NEED_LENGTH 			= -1;
	public static final int DEFAULT_TEXT_LENGTH		= 4000;
	public static final int DEFAULT_STRING_LENGTH	= 255;
	public static final int DEFAULT_PRECISION 		= 19;
	public static final int DEFAULT_SCALE 			= 4;
	
	// -------------------------------------------------------------------------------------
	
	public static final Type PRIMARY 		= new FieldType(10001, "primary", new PrimaryEditor());
	public static final Type REFERENCE 		= new FieldType(10002, "reference", new ReferenceEditor());
	public static final Type REFERENCE_LIST = new FieldType(10003, "reference-list", new ReferenceListEditor());

	public static final Type INT 			= new FieldType(10011, "int", new IntEditor());
	public static final Type SMALL_INT 		= new FieldType(10012, "small-int", new SmallIntEditor());
	public static final Type TINY_INT 		= new FieldType(10013, "tiny-int", new TinyIntEditor());
	public static final Type DOUBLE 		= new FieldType(10014, "double", new DoubleEditor());
	public static final Type DECIMAL 		= new FieldType(10015, "decimal", new DecimalEditor());
	public static final Type LONG			= new FieldType(10016, "long", new LongEditor());

	public static final Type DATE 			= new FieldType(10021, "date", new DateEditor());
	public static final Type DATETIME 		= new FieldType(10022, "datetime", new DateTimeEditor());
	public static final Type TIMESTAMP 		= new FieldType(10023, "timestamp", new TimestampEditor());

	public static final Type CHAR 			= new FieldType(10031, "char",	new CharEditor());
	public static final Type STRING 		= new FieldType(10032, "string",	new StringEditor());
	public static final Type TEXT 			= new FieldType(10033, "text",	new TextEditor());

	public static final Type BOOL 		= new FieldType(10091, "bool",	new BoolEditor());
	public static final Type NTEXT 		= new FieldType(10092, "ntext", new NTextEditor());
	public static final Type BINARY 	= new FieldType(10093, "binary",	new BinaryEditor());

	// -------------------------------------------------------------------------------------
	
	final private Integer mask;
	final private String name;
	final private Editor editor;

	private FieldType(Integer mask, String name, Editor editor) {
		this.mask = mask;
		this.name = name;
		this.editor = editor;
	}

	/**
	 */
	public Integer getMask() {
		return mask;
	}

	/**
	 */
	public String getName() {
		return name;
	}

	/**
	 */
	public Editor getFieldEditor() {
		return editor;
	}

	@Override
	public int hashCode() {
		return super.hashCode() >>> getMask();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Type))
			return false;

		Type t2 = (Type) obj;
		return t2.getMask() == this.getMask();
	}

	@Override
	public String toString() {
		return "<" + mask + ':' + name + '>';
	}
}
